import java.security.SecureRandom;
import java.util.Scanner;

public class PasswordGenerator {
    private static final String UPPER_CASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER_CASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
    private static final String NUMERIC_CHARACTERS = "0123456789";

    public void lenth(int passwordLength){
        String generatedPassword = generatePassword(passwordLength);
        System.out.println("生成的密码为：" + generatedPassword);
    }
    private static String generatePassword(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder password = new StringBuilder();

        // 生成至少一个大写字母
        password.append(UPPER_CASE_CHARACTERS.charAt(random.nextInt(UPPER_CASE_CHARACTERS.length())));

        // 生成至少一个小写字母
        password.append(LOWER_CASE_CHARACTERS.charAt(random.nextInt(LOWER_CASE_CHARACTERS.length())));

        // 生成至少一个数字
        password.append(NUMERIC_CHARACTERS.charAt(random.nextInt(NUMERIC_CHARACTERS.length())));

        // 生成剩余字符
        for (int i = 0; i < length - 3; i++) {
            String characters = UPPER_CASE_CHARACTERS + LOWER_CASE_CHARACTERS + NUMERIC_CHARACTERS;
            password.append(characters.charAt(random.nextInt(characters.length())));
        }

        // 打乱密码中字符的顺序
        for (int i = 0; i < password.length(); i++) {
            int randomIndex = random.nextInt(password.length());
            char temp = password.charAt(i);
            password.setCharAt(i, password.charAt(randomIndex));
            password.setCharAt(randomIndex, temp);
        }

        return password.toString();
    }
}
